    //
//  Screen.m
//  iphone_recharge
//
//  Created by xuyue on 12-8-16.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "Screen.h"
#import "iToast.h"
#import "ZZTongDefine.h"
#import "DefineUtil.h"
#import "NSNetLib.h"
#import "ZZTongUIEngine.h"
#import "luaEngine.h"
#import "HPClientEngine.h"
#import "ZZTongDefine.h"

#define AnimationDuration 0.2

@implementation Screen
@synthesize screenData;
@synthesize screenName;
@synthesize canSave;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)restartAnimation
{
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.isDisappearing = NO;
    Iphone7ViewRectDown;
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.isDisappearing = YES;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.isDisappearing = NO;
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView{
}

- (void)dealloc {
    self.screenData = nil;
    self.screenName = nil;
    [super dealloc];
}

#pragma mark item delegate

-(void)popUI:(CGSize)keyboardSize{
    if (inputView.inputView) {
        keyboardSize = inputView.inputView.frame.size;
    }
    if (inputView.inputAccessoryView) {
        keyboardSize.height += inputView.inputAccessoryView.frame.size.height;
    }
    
    CGPoint point=CGPointMake(0, inputView.frame.size.height);
    
    point=[inputView convertPoint:point toView:self.view];
    CGSize screenSize=self.view.frame.size;
    
    if ([self.view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)self.view;
        CGPoint offsetPoint = scrollView.contentOffset;
        offsetPoint.y = (point.y - (screenSize.height-keyboardSize.height));
        
        if (offsetPoint.y <= 0) {
            offsetPoint.y = 0;
        }
        
        [scrollView setContentOffset:offsetPoint animated:YES];
        return;
    }
    
    CGRect rr = CGRectZero;
    CGFloat osOffset = 0;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        if (self.edgesForExtendedLayout == UIRectEdgeNone
            && !self.navigationController.navigationBarHidden) {
            osOffset = IOS7_VIEW_FRAME_Y;
        }
    }
    
    if ((screenSize.height-keyboardSize.height)<point.y) {
        rr=CGRectMake(0, (screenSize.height-keyboardSize.height)-point.y + osOffset, screenSize.width, screenSize.height);
    }else{
        rr=CGRectMake(0, osOffset, screenSize.width, screenSize.height);
    }
    
    // for extra height
    CGFloat extraHeight = 0;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    if ([inputView respondsToSelector:@selector(getKeyBoardExtraHeight)]) {
        SEL selector = @selector(getKeyBoardExtraHeight);
#pragma clang diagnostic pop
        NSMethodSignature * sig = [[inputView class] instanceMethodSignatureForSelector: selector];
        NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature:sig];
        [myInvocation setTarget:inputView];
        [myInvocation setSelector:selector];
        [myInvocation invoke];
        [myInvocation getReturnValue: &extraHeight];
    }
    
    if (extraHeight > 0) {
        CGFloat tempY = point.y - inputView.frame.size.height - osOffset;
        tempY = tempY + rr.origin.y - extraHeight;
        if (tempY > 0){
            rr.origin.y -= extraHeight;
        }else{
            rr.origin.y -= (extraHeight + tempY);
        }
        
//        if (point.y - rr.origin.y - inputView.frame.size.height - osOffset > 0) {
//            rr.origin.y -= extraHeight;
//        }else{
//            rr.origin.y = rr.origin.y - extraHeight - (point.y - rr.origin.y - inputView.frame.size.height - osOffset);
//        }
    }
    
    [UIView beginAnimations:@"frameChange" context:nil];
    [UIView setAnimationDuration:AnimationDuration];
    self.view.frame=rr;
    [UIView commitAnimations];
}
- (void) textFieldBeginEditingForTextField:(UIView *)textView
{
    BOOL flag = NO;     // 是否是从另一个text field直接进入下一个text field，这样不会发出键盘弹出的通知，要手动调用
    if (inputView != nil) {
        flag = YES;
    }
    inputView = textView;
    if (flag) {
        CGSize tarSize = CGSizeEqualToSize(HPUIENG.keyBoardSize, CGSizeZero) ? kKeyboardSize : HPUIENG.keyBoardSize;
        [self popKeyBoardUI:tarSize];
    }
}

/*-(void)DoHideKeyboard{
    [inputTextfield resignFirstResponder];
}*/

-(void)hideKeyboard{
    /*if (inputTextfield != nil) {
        [self performSelectorOnMainThread:@selector(DoHideKeyboard) withObject:nil waitUntilDone:YES];
    }*/
    if (inputView != nil){
        [inputView resignFirstResponder];
    }
}

-(void)popKeyBoardUI:(CGSize)keyboardSize{
    if (inputView != nil) {
        [self popUI:keyboardSize];
    }
}

-(void)hideKeyBoardUI{
	CGSize size=self.view.frame.size;
    [UIView beginAnimations:@"frameChange" context:nil];
    [UIView setAnimationDuration:AnimationDuration];
    
    CGFloat osOffset = 0;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        if (self.edgesForExtendedLayout == UIRectEdgeNone && !self.navigationController.navigationBarHidden) {
            osOffset = IOS7_VIEW_FRAME_Y;
        }
    }
    
	CGRect rr=CGRectMake(0, osOffset, size.width, size.height);
    
	self.view.frame=rr;
    [UIView commitAnimations];
    if ([inputView isKindOfClass:[UITextView class]]) {
        ((UITextView*)inputView).inputAccessoryView = nil;
    }else if ([inputView isKindOfClass:[UITextField class]]){
        ((UITextField*)inputView).inputAccessoryView = nil;
    }
    [inputView resignFirstResponder];
    LOG(@"hideKeyBoardUI::%@",inputView);
    inputView = nil;
}
//-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}
//
//-(BOOL)shouldAutorotate
//{
//    return NO;
//}
//
//-(NSUInteger)supportedInterfaceOrientations
//{
//    return UIInterfaceOrientationMaskPortrait;
//}

//- (void) setCurrentInputTextField:(UITextField *)textField
//{
//    BOOL flag = NO;     // 是否是从另一个text field直接进入下一个text field，这样不会发出键盘弹出的通知
//    if (inputTextfield != nil) {
//        flag = YES;
//    }
//    inputTextfield = textField;
//    if (flag) {
//        [self popKeyBoardUI:kKeyboardSize];
//    }
//}
//
//- (UITextField *) currentInputTextField
//{
//    return inputTextfield;
//}

@end
